/***************************************
 * Created by CK on 2016/7/12.
 * 重复用到的方法
 ***************************************/
$.overtFun = {
    clickSubmitForm: function(_list) {
        if(!_list) _list = $('#List');
        $('#xh_form').submit();
        $('.xh-form').hide();
        $('.xh-datagrid').parent().fadeIn();
        $('body').css('overflow-y', 'hidden');
        _list.dataGridReload();
    },
    clickBackForm: function(){
        $('.xh-form').hide();
        $('.xh-datagrid').parent().fadeIn();
        $('body').css('overflow-y', 'hidden');
        $('#bt_form_reset').click();
    },
    clickResetForm: function(){
        $('#bt_form_add, #bt_form_edit').setButtonDisable('bs');
        $('.xh-form').resetForm().parseForm();
    },
    clickAddList: function(){
        $('.xh-datagrid').parent().hide();
        $('#bt_form_reset').click();
        $('.xh-form').fadeIn();
        $('#bt_form_edit').hide();
        $('body').css('overflow-y', 'auto');
        $('#bt_form_reset, #bt_form_add').show();
    },
    clickDelList: function(_ids, _url, _list, _msg){
        if($('#bt_list_del').checkButtonDisabled()) return false;
        var _params = {"_ids": _ids};
        if(_url){
            _params._url = _url;
        }
        if(_list){
            _params._list = _list;
        }
        xhExtra.showConfirm({title: 'Warning', content: _msg ? _msg : '确认删除？'}, $.overtFun.confirmDel, _params);
    },
    confirmDel: function(_json){
        if(!_json) return false;
        //if(!_list) _list = $('#List');
        if(!_json._list) _json._list = $('#List');
        var delUrl = 'delData';
        if(_json._url  && _json._url != 'undefined') delUrl = _json._url;
        $.post(delUrl, {ids: _json._ids}, function(result){
            xhExtra.showMessage({title: '结果', content: result});
            _json._list.dataGridReload();
            //$('#bt_list_del').setButtonDisable('eu');
        });
    },
    openEditPage: function(_id, _target, _callback, _callJson) {
        $('.xh-datagrid').parent().hide();
        $.post(baseUrl + 'Other/createFormWithData', {id: _id, target: _target}, function(form){
            $('.xh-area-form').html(form).find('[must=true]').each(function(){
                $(this).parent().removeClass('has-warning').addClass('has-success');
            });
            $('.xh-form').parseForm($('#bt_form_add, #bt_form_edit')).fadeIn();
            $('#bt_form_edit').setButtonEnable('bs').show();
            $('#bt_form_add, #bt_form_reset').hide();
            $('body').css('overflow-y', 'auto');
            if(_callback) _callback.call(this, _callJson);          //如果有回调则调用
        });
    },
    setModelState: function(_url, _ids, _target, _list) {
        if(!_list) _list = $('#List');
        $.post(_url, {ids: _ids, target: _target}, function(result){
            xhExtra.showMessage({title: '结果', content: result});
            _list.dataGridReload();
        });
    },
    /**
     * datagrid添加模糊参数，如有传入参数，则合并
     *
     * @param _event
     * @param _queryParams
     * @param _searchDom
     * @param _searchBtn
     //* @param _jsonDom
     * @returns {boolean}
     *
     * create by ck 20160929
     * modify by ck 20161008    点击按钮搜索，绑定回车事件
     * modify by ck 20161010    添加自定义按钮，去除绑定回车事件
     * modify by ck 20161011    添加jsonDom，多个搜索节点
     */
    vagueSearch: function(_event , _queryParams, _searchDom, _searchBtn){
        if(!_searchDom) _searchDom = $('#list_search');
        if(!_searchBtn) _searchBtn = $('#bt_list_search');
        if(_searchDom.length <= 0 || _searchBtn.length <= 0) return false;
        _searchBtn.click(function(){
            var _search = {"vague" : _searchDom.getElementValue()};
            //if(_jsonDom){
            //    var _json = {};
            //    $.each(_jsonDom, function(key, dom){
            //        _json[key] = dom.getElementValue();
            //    });
            //    _search = mergeJsonObject(_search, _json);
            //}
            if(_queryParams){
                //_queryParams = mergeJsonObject(_queryParams, {"search": _search});
                _queryParams = $.extend({}, _queryParams, {"search": _search});
            }
            else {
                _queryParams = {"search": _search};
            }
            _event.addDataGridParams(_queryParams);
        });
        _searchDom.textbox('textbox').on('keydown', function(event) {
            if(event.keyCode == 13) {
                _searchBtn.click();
            }
        });
    }
};
